﻿using System.Windows;
using System.Windows.Controls;
using Infragistics.Composite.Wpf.Docking;
using Infragistics.Composite.Wpf.Proxies.Docking;
using Infragistics.Windows.DockManager;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Infragistics.Composite.Wpf.Tests.Docking
{
    /// <summary>
    /// Unit tests for the XamDockManagerRegion class.
    /// </summary>
    [TestClass]
    public class XamDockManagerRegionTests
    {
        #region Boilerplate Code

        public XamDockManagerRegionTests()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #endregion // Boilerplate Code

        [TestMethod]
        public void TestAddWithoutSettings()
        {
            XamDockManagerRegion target = new XamDockManagerRegion();
            XamDockManager dockManager = new XamDockManager();
            target.DockManagerProxy = new XamDockManagerProxy(dockManager);

            DependencyObject view = new DependencyObject();

            target.Add(view);

            Assert.IsTrue(target.Views.Contains(view), "View was not added to region.");
            Assert.IsTrue(dockManager.Panes.Count == 1, "Pane was not created for view.");
            Assert.IsTrue(dockManager.Panes[0].Panes[0] is TabGroupPane, "TabGroupPane was not created for view.");
            Assert.IsTrue(((dockManager.Panes[0].Panes[0] as TabGroupPane).Items[0] as ContentPane).Content == view, "ContentPane does not contain view.");
        }

        [TestMethod]
        public void TestAddWithSplitPaneNameSpecified()
        {
            XamDockManagerRegion target = new XamDockManagerRegion();
            XamDockManager dockManager = new XamDockManager();
            target.DockManagerProxy = new XamDockManagerProxy(dockManager);

            DependencyObject view = new DependencyObject();
            XamDockManagerSettings.SetSplitPaneName(view, "SOME_NAME");

            target.Add(view);

            SplitPane splitPane = dockManager.Panes[0];
            Assert.AreEqual("SOME_NAME", splitPane.Name, "SplitPane name was not set.");
        }

        [TestMethod]
        public void TestAddWithSplitPaneStyleSpecified()
        {
            XamDockManagerRegion target = new XamDockManagerRegion();
            XamDockManager dockManager = new XamDockManager();
            target.DockManagerProxy = new XamDockManagerProxy(dockManager);

            DependencyObject view = new DependencyObject();
            Style style = new Style { TargetType = typeof(SplitPaneProxy) };
            style.Setters.Add(new Setter(SplitPane.SplitterOrientationProperty, Orientation.Horizontal));
            XamDockManagerSettings.SetSplitPaneProxyStyle(view, style);

            target.Add(view);

            SplitPane splitPane = dockManager.Panes[0];
            Assert.AreEqual(Orientation.Horizontal, splitPane.SplitterOrientation, "Style was not applied.");
        }

        [TestMethod]
        public void TestAddWithContentPaneStyleSpecified()
        {
            XamDockManagerRegion target = new XamDockManagerRegion();
            XamDockManager dockManager = new XamDockManager();
            target.DockManagerProxy = new XamDockManagerProxy(dockManager);

            DependencyObject view = new DependencyObject();
            Style style = new Style { TargetType = typeof(ContentPaneProxy) };
            style.Setters.Add(new Setter(ContentPane.HeaderProperty, "SOME HEADER TEXT"));
            XamDockManagerSettings.SetContentPaneProxyStyle(view, style);

            target.Add(view);

            ContentPane contentPane = (dockManager.Panes[0].Panes[0] as TabGroupPane).Items[0] as ContentPane;
            Assert.AreEqual("SOME HEADER TEXT", contentPane.Header, "Style was not applied.");
        }

        [TestMethod]
        public void TestAddWithContentPaneNotInTabGroup()
        {
            XamDockManagerRegion target = new XamDockManagerRegion();
            XamDockManager dockManager = new XamDockManager();
            target.DockManagerProxy = new XamDockManagerProxy(dockManager);

            DependencyObject view = new DependencyObject();
            XamDockManagerSettings.SetIsContentPaneInTabGroup(view, false);

            target.Add(view);

            Assert.IsTrue(dockManager.Panes[0].Panes[0] is ContentPane, "ContentPane should exist.");
        }

        [TestMethod]
        public void TestAddTwoViews()
        {
            XamDockManagerRegion target = new XamDockManagerRegion();
            XamDockManager dockManager = new XamDockManager();
            target.DockManagerProxy = new XamDockManagerProxy(dockManager);

            DependencyObject view1 = new DependencyObject();
            XamDockManagerSettings.SetIsContentPaneInTabGroup(view1, false);
            XamDockManagerSettings.SetSplitPaneName(view1, "PANE1");

            DependencyObject view2 = new DependencyObject();
            XamDockManagerSettings.SetIsContentPaneInTabGroup(view2, true);
            XamDockManagerSettings.SetSplitPaneName(view2, "PANE1");

            target.Add(view1);
            target.Add(view2);

            Assert.AreEqual(dockManager.Panes[0].Panes.Count, 2, "Two panes should exist.");
            Assert.IsTrue(dockManager.Panes[0].Panes[0] is ContentPane, "First pane should be a ContentPane.");
            Assert.AreEqual(view1, (dockManager.Panes[0].Panes[0] as ContentPane).Content, "First ContentPane should contain view1.");
            Assert.IsTrue(dockManager.Panes[0].Panes[1] is TabGroupPane, "Second pane should be a TabGroupPane.");
            Assert.IsTrue((dockManager.Panes[0].Panes[1] as TabGroupPane).Items[0] is ContentPane, "Pane in tab group should be a ContentPane.");
            Assert.AreEqual(view2, ((dockManager.Panes[0].Panes[1] as TabGroupPane).Items[0] as ContentPane).Content, "Second ContentPane should contain view2.");
        }
    }
}